<?php

namespace frontend\modules\resource\controllers;

use frontend\modules\common\controllers\BaseController;
use frontend\modules\common\models\BaseHelper;
use frontend\modules\common\models\BaseParam;
use frontend\modules\resource\logic\UserBranchLogic;
use Yii;
use common\models\BasMember;
use frontend\modules\resource\models\BasMemberSearch;
use yii\web\NotFoundHttpException;

/**
 * BranchUserController implements the CRUD actions for BasMember model.
 */
class BranchUserController extends BaseController
{
    /**
     * @throws \yii\base\ExitException
     */
    public function init()
    {

        parent::init(); // TODO: Change the autogenerated stub
        $this->checkBrId('BranchIdForBranchUser');
    }

    /**
     * Lists all BasMember models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new BasMemberSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $this->branchId);


        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'branchId' => $this->branchId
        ]);
    }

    /**
     * Displays a single BasMember model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView()
    {
        $id = Yii::$app->request->get('id');
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new BasMember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new BasMember();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing BasMember model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete()
    {
        $id = Yii::$app->request->post('id');

        //不是数字 返回错误
        if (!is_numeric($id)) return $this->err();
        $this->findModel($id)->delete();

        //如果是ajax请求
        if (Yii::$app->request->isAjax) return $this->suc();
        return $this->redirect(['index']);
    }

    /**
     * Finds the BasMember model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return BasMember the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = BasMember::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**
     * 用户账号状态该
     * @return string
     * @throws \yii\base\ExitException
     */
    public function actionUserStatus()
    {
        $id = Yii::$app->request->post('id');
        $res = (new UserBranchLogic())->UpdateUserStatus($id, $this->branchId);
        if (!$res) {
            BaseHelper::Error('修改失败');
        }
        return $this->suc(['msg' => '修改成功']);

    }

    /**
     * @return string
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \yii\base\ExitException
     * @throws \yii\db\Exception
     */
    public function actionImportExcel()
    {

        $file = Yii::$app->request->post('file');
        $num = (new UserBranchLogic())->ImportData(
            BaseParam::$excelSavePath . $file,
            BaseParam::$UserExcelHeader,
            $this->branchId,
            $this->branchTitle
        );
        return $this->suc(['msg' => '导入记录：' . $num . ' 条']);
    }

}
